/*********************************************************************************************************
* Movement in form of a rectangle and gives it back
*
*  Author of the ported code: Florian Weil - www.derhess.de
*
************************************************************************************************************/

inlets = 2;
outlets = 2;

// distance between two points or coordinate
var velocity = 10;
var direction = "down"; // "up", "right", "left"
var directionList = new Array("up","down","right","left");

var canvas_width = 640;
var canvas_height = 480;

var x_pos = 10;
var y_pos = 10;
var new_x_pos = x_pos;
var new_y_pos = y_pos;


function setVelocity(value)
{
	if(value > 0)
		velocity = value;
	else
		post("ERROR: velocity value is negative. It must be greater than 0!\n");
}

function setDirection(value)
{
	switch(value)
		{
			case "up":
			  direction = value;
			  break;
			case "down":
			  direction  = value;
			  break;
			case "right":
			  direction = value;
			  break;
			case "left":
			  direction = value;
			  break;
			
			default:
			  post("ERROR: direction has got a wrong value. Only 'up', 'down','left', and 'right' is allowed!\n");
		}
}

function setDirectionIndex(value)
{
	if(value < 0 || value > 3) {
		post("ERROR: directionIndex has got a wrong value. Only values between 0 and 3 are allowed!\n");
	} else {
		direction = directionList[value];
	}
}


function move() 
{
	new_x_pos = x_pos;
	new_y_pos = y_pos;
	
	moveCursor();
}

function reset()
{
	//velocity = 10;
	direction = "down"; // "up", "right", "left"
	x_pos = 10;
	y_pos = 10;
}

//////////////////////////////////////////////////////////////////////////
//    MAX MSP FUNCTION
//////////////////////////////////////////////////////////////////////////
function bang()
{
	move();
	output();
}

//////////////////////////////////////////////////////////////////////////
//    Helper Functions
//////////////////////////////////////////////////////////////////////////
function getRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
 
    return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

function moveCursor() 
{
	var changeDirection = false;
	
	switch(direction)
	{
		case "up":
		  new_y_pos -= velocity;
		  break;
		case "down":
		  new_y_pos += velocity;
		  break;
		case "right":
		  new_x_pos += velocity;
		  break;
		case "left":
		  new_x_pos -= velocity;
		  break;
		default:
		  post("ERROR: Movement is shit. Something went wrong. Please hit the Programmer!\n");
	}
	
	//checking boundaries
	if(new_x_pos < velocity) {
		new_x_pos = x_pos;
		changeDirection = true;
	} else if(new_x_pos > (canvas_width - velocity)) {
		new_x_pos = x_pos;
		changeDirection = true;
	} else if(new_y_pos < velocity) {
		new_y_pos = y_pos;
		changeDirection = true;
	} else if(new_y_pos > (canvas_height - velocity)) {
		new_y_pos = y_pos;
		changeDirection = true;
	} 
	
	if(changeDirection) {
		findDirection();
		moveCursor();
	} else {
		x_pos = new_x_pos;
		y_pos = new_y_pos;
	}
		
}

function findDirection() 
{
	var tempDirection = getRandom(0,3);
	post("tempDirection ID: " + tempDirection + " name: " +  directionList[tempDirection]);
	if(direction == directionList[tempDirection]) {
		post("Direction is the same. Search for a new one\n");
		findDirection();
	} else {
		direction = directionList[tempDirection];
	}
}

function output()
{
	outlet(0, x_pos);
    outlet(1, y_pos);
	//post("rectangleMovement: Send Bang: " + x_pos + "/" + y_pos + "\n");
}
